package com.ad.pet.common.structure;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;

import com.ad.pet.common.data.utils.DensityUtil;

import java.io.File;

/**
 * Created by z on.
 * Des:照片管理类
 * Date: 2017/3/15
 */

public class PhotoManager {
    public final static int REQUEST_CODE_CHECK_IMAGE = 0x110;
    public final static int REQUEST_CODE_OPEN_CAMERA = 0x111;
    public final static int REQUEST_CODE_OPEN_GALLERY_AND_CROP = 0x112;
    public final static int REQUEST_CODE_CROP_CODE = 0x113;

    private final static String FILE_PROVIDER = "com.ad.pet.PhotoFileProvider";


    /**
     * 打开相机
     *
     * @param activity
     * @param file
     * @param requestCode
     * @return
     */
    public static File openCamera(Activity activity, File file, int requestCode) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uriForFile;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0及以上
            uriForFile = FileProvider.getUriForFile(activity, FILE_PROVIDER, file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            uriForFile = Uri.fromFile(file);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
        activity.startActivityForResult(intent, requestCode);
        return file;
    }

    /**
     * 打开相机
     *
     * @param fragment
     * @param file
     * @param requestCode
     * @return
     */
    public static File openCamera(Fragment fragment, File file, int requestCode) {
        Intent intent = new Intent();
        intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri uriForFile;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0及以上
            uriForFile = FileProvider.getUriForFile(fragment.getActivity(), FILE_PROVIDER, file);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            uriForFile = Uri.fromFile(file);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
        fragment.startActivityForResult(intent, requestCode);
        return file;
    }

    /**
     * 打开图库
     *
     * @param fragment
     * @param requestCode
     */
    public static void openGallery(Fragment fragment, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 打开图库
     *
     * @param activity
     * @param requestCode
     */
    public static void openGallery(Activity activity, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK);
        intent.setType("image/*");
        activity.startActivityForResult(intent, requestCode);
    }


    /**
     * uri 转 filePath
     *
     * @param context
     * @param uri
     * @return
     */
    public static String uriToUrl(final Context context, final Uri uri) {
        ContentResolver resolver =
                context.getContentResolver();
        //创建查询对象
        Cursor data = resolver.query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
        if (data != null) {
            //获取路径
            if (data.moveToFirst()) {
                int imageIndex = data.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                String path = data.getString(imageIndex);
                return path;
            }
            if (data != null) data.close();
        }
        return "";
    }

    /**
     * fragment 启动方式  输出为uri
     *
     * @param fragment
     * @param file
     * @param requestCode
     */
    public static void cropGallery(Fragment fragment, File file, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            openGallery(fragment, file, requestCode);
        } else {
            cropGallery(fragment, file, DensityUtil.getScreenSize().x, (int) (DensityUtil.getScreenSize().y * 0.7), requestCode);
        }
    }

    /**
     * 打开相册并裁剪
     *
     * @param fragment
     * @param file
     * @param outputX
     * @param outputY
     * @param requestCode
     */
    private static void cropGallery(Fragment fragment, File file, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 2);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", false); // no face detection
        fragment.startActivityForResult(intent, requestCode);
    }


    /**
     * 打开相册并裁剪
     *
     * @param activity
     * @param file
     * @param outputX
     * @param outputY
     * @param requestCode
     */
    private static void cropGallery(Activity activity, File file, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        intent.setType("image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 2);
        intent.putExtra("aspectY", 2);
        intent.putExtra("outputX", outputX);
        intent.putExtra("outputY", outputY);
        intent.putExtra("scale", true);
        intent.putExtra("return-data", false);
        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
        intent.putExtra("noFaceDetection", false); // no face detection
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 打开相册
     *
     * @param fragment
     * @param file
     * @param requestCode
     */
    public static void openGallery(Fragment fragment, File file, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        intent.setType("image/*");
        Uri uriForFile;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uriForFile = FileProvider.getUriForFile(fragment.getActivity(), "com.ems358.cms.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uriForFile = Uri.fromFile(file);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 打开相册
     *
     * @param activity
     * @param file
     * @param requestCode
     */
    public static void openGallery(Activity activity, File file, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
        intent.setType("image/*");
        Uri uriForFile;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            uriForFile = FileProvider.getUriForFile(activity, "com.ems358.cms.fileprovider", file);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        } else {
            uriForFile = Uri.fromFile(file);
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uriForFile);
        activity.startActivityForResult(intent, requestCode);
    }

    /**
     * 打开相册并裁剪 如果Android版本7.0及以上则只打开相册
     *
     * @param activity
     * @param file
     * @param requestCode
     */
    public static void cropGallery(Activity activity, File file, int requestCode) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            openGallery(activity, file, requestCode);
        } else {
            cropGallery(activity, file, DensityUtil.getScreenSize().x, (int) (DensityUtil.getScreenSize().y * 0.7), requestCode);
        }
    }


    public static void crop(Activity activity, File saveFile, File dataFile, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri dataUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            dataUri = FileProvider.getUriForFile(activity, FILE_PROVIDER, dataFile);
            intent.putExtra("noFaceDetection", false);//去除默认的人脸识别，否则和剪裁匡重叠
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            dataUri = Uri.fromFile(dataFile);
        }
        intent.setDataAndType(dataUri, "image/*");
        intent.putExtra("crop", "true"); // 开启剪裁
        intent.putExtra("aspectX",outputX ); // 宽高比例
        intent.putExtra("aspectY", outputY);
        intent.putExtra("outputX", outputX); // 宽高
        intent.putExtra("outputY", outputY);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(saveFile));
        activity.startActivityForResult(intent, requestCode);
    }

    public static void crop(Fragment fragment, File saveFile, File dataFile, int outputX, int outputY, int requestCode) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        Uri dataUri;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            dataUri = FileProvider.getUriForFile(fragment.getActivity(), FILE_PROVIDER, dataFile);
            intent.putExtra("noFaceDetection", false);//去除默认的人脸识别，否则和剪裁匡重叠
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            dataUri = Uri.fromFile(dataFile);
        }
        intent.setDataAndType(dataUri, "image/*");
        intent.putExtra("crop", "true"); // 开启剪裁
        intent.putExtra("aspectX", outputX); // 宽高比例
        intent.putExtra("aspectY", outputY);
        intent.putExtra("outputX", outputX); // 宽高
        intent.putExtra("outputY", outputY);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(saveFile));
        fragment.startActivityForResult(intent, requestCode);
    }

    /**
     * 裁剪
     *
     * @param activity    上下文
     * @param saveFile    保存的路径
     * @param dataFile    原图片的路径
     * @param requestCode
     */
    public static void crop(Activity activity, File saveFile, File dataFile, int requestCode) {
        crop(activity, saveFile, dataFile, DensityUtil.getScreenSize().x, (int) (DensityUtil.getScreenSize().y * 0.7), requestCode);
    }


    /**
     * 裁剪
     *
     * @param fragment    上下文
     * @param saveFile    保存的路径
     * @param dataFile    原图片的路径
     * @param requestCode
     */
    public static void crop(Fragment fragment, File saveFile, File dataFile, int requestCode) {
        crop(fragment, saveFile, dataFile, DensityUtil.getScreenSize().x, (int) (DensityUtil.getScreenSize().y * 0.7), requestCode);
    }
}
